<?php
/**
 * LICENSE
 *
 * Copyright (C) 2010 Nicholas Miller
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or any
 * later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * BEFORE USING THIS FILE MAKE SURE YOU HAVE READ THE README.txt FILE INCLUDED
 * DIRECTLY UNDER THE "examples" DIRECTORY
 *
 * This is a complete example of how to perform onboarding, over SOAP, with this API
 * 
 * @author Nicholas Miller <nicholas.j.miller@gmail.com>
 */

$paths = array(
    get_include_path(),
    realpath(dirname(__FILE__) . '/../../library')
);

set_include_path(implode(PATH_SEPARATOR, $paths));

// Here we're using Zend's autoloader library so we don't have
// to place "require_once" statements throughout the library.
require_once 'Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace('PayPalSoap_');

// Loading the config file which contains the api information you supplied
$configFile = dirname(__FILE__) . '/../common/config.ini';
$config = new Zend_Config_Ini($configFile);

// All credentials are stored neatly within one credential object
$credentials = new PayPalSoap_Credentials(
    $config->paypal->username,
    $config->paypal->password,
    $config->paypal->signature
);

// All endpoint information is also neatly bundled into one object
$endpoint = new PayPalSoap_Endpoint(
    $config->paypal->useSandbox 
        ? PayPalSoap_Endpoint::ENV_SANDBOX
        : PayPalSoap_Endpoint::ENV_LIVE,
    PayPalSoap_Endpoint::URL_API_AA_THREE_TOKEN
);

// Now, build the SOAP client to interface with PayPal
// This client is simply an extension of Zend's soap client
$client = new PayPalSoap_Client($credentials, $endpoint);

// Provide the detailed information required for onboarding
$details = new PayPalSoap_Type_EnterBoardingRequestDetails();
$details->ProgramCode = $config->paypal->onboardingProgramCode;
$details->ProductList = 'ec,auth_settle,admin_api';
$details->MarketingCategory = 'Marketing-Category1';
$details->ImageUrl = 'http://www.example.com/images-1.jpg';

// Place the details of the boarding request within the request object.
$request = new PayPalSoap_Type_EnterBoardingRequest();
$request->EnterBoardingRequestDetails = $details;

// Now call the EnterBoarding method on the client.
// Each request method in the client uses type hinting, to make sure
// a request is paired with the correct desired action
$response = $client->EnterBoarding($request);

// Finally output the token.
echo 'Your onboarding token is: ' . $response->Token . "\n\n";